package com.party.mobile.biz.article;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.party.core.model.channel.Channel;
import com.party.core.model.subject.SubjectApply;
import com.party.core.service.channel.IChannelService;
import com.party.core.service.subject.ISubjectApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.party.core.exception.BusinessException;
import com.party.core.model.BaseModel;
import com.party.core.model.article.Article;
import com.party.core.model.article.ArticleGroup;
import com.party.core.service.article.IArticleGroupService;
import com.party.core.service.article.IArticleService;
import com.party.mobile.web.dto.article.output.ArticleGroupOutput;
import com.party.common.utils.PartyCode;

@Service
public class ArticleGroupBizService {

	@Autowired
	IArticleGroupService articleGroupService;

	@Autowired
	IArticleService articleService;
	@Autowired
	IChannelService channelService;


	@Autowired
	private ISubjectApplyService subjectApplyService;

	public ArticleGroupOutput getDetails(String id, HttpServletRequest request) {
		ArticleGroup channel = articleGroupService.get(id);
		if (channel == null) {
			throw new BusinessException(PartyCode.DYNAMIC_NOT_EXIT, "动态的主键id错误，数据不存在");
		}
		SubjectApply subjectApply = subjectApplyService.findByTargetId(id);
		ArticleGroupOutput output = ArticleGroupOutput.transform(channel);

		//栏目下分类
		Channel cateGory = new Channel();
		cateGory.setMember(subjectApply.getId());
		cateGory.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
		List<Channel> channelList = channelService.listPage(cateGory, null);

		if (channelList.size() == 1 || channelList.size() == 0) {
			//1 文章  2  分类
			output.setIsArticleOrCategory(Integer.valueOf(1));
		}
		if (channelList.size() > 1) {
			output.setIsArticleOrCategory(Integer.valueOf(2));
		}

		Article article = new Article();
		article.setArticleGroupId(id);
		article.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
		List<Article> articles = articleService.list(article);
		output.setArticleNum(articles.size());
		output.setName(subjectApply.getName());
		output.setRemarks(subjectApply.getRemarks());
		return output;
	}

}
